<?php
/* 
 *采购详情
 */
class Buy_detailAction extends TransDetailAction {
    public function _filter(&$map) {
        $key = trim($_REQUEST['keywords']);
        
        if(!empty($key)) {
            if(strpos($key, ' ')) //高级搜索，仅搜索产品名称
                $map['_complex'] = A('Search')->search($key,'Product','name','Product.id');
            else{
                $where['Product.name'] = array('like',"%".$key."%");//产品名称
                $where['_logic'] = "or";
                $map['_complex'] = $where;
            }
            
        }
        if(!empty($_REQUEST['buy_id'])){
           $map['buy_id'] = $_REQUEST['buy_id'];
        }

        if(!empty($_REQUEST['buy_no'])){
            $where['Buy.buy_no'] = array('like',"%".trim($_REQUEST['buy_no'])."%");
        }

        if( $_REQUEST['supplier_id'] != '' )
          $map['supplier_id'] = $_REQUEST['supplier_id'];

        $cond_time = array();
        if( $_REQUEST['start_time'] != '' )
          array_push($cond_time, array( 'egt' , strtotime($_REQUEST['start_time']) ) );
        if( $_REQUEST['end_time'] != '' )
          array_push($cond_time, array( 'elt' , strtotime($_REQUEST['end_time']) ) );
      if(count($cond_time) >0 )
         $map['create_time'] = $cond_time;

    }

    public function index(){
        $map = $this->_search ();
        if (method_exists ( $this, '_filter' )) {
            $this->_filter ( $map );
        }

        $this->assign('meta_title','采购明细');
        if(empty($_REQUEST['keywords'])){
            $sum_overview = M('Buy_detail')->where($map)->field('sum(number) as sum_number,sum(total) as sum_total')->find();
            $this->assign('sum_overview',$sum_overview);
            parent::index();
        }
        else{
            $sum_overview = D('Buy_detailView')->where($map)->field('sum(number) as sum_number,sum(total) as sum_total')->find();
            $this->assign('sum_overview',$sum_overview);
            parent::index('Buy_detailView');
        }
            
    }

    public function _before_add() {
        $supplier = M('Supplier')->where('status=1')->field('id,name')->findAll();
        $this->assign('supplier',$supplier);
    }

}
?>
